# Built-in topographic data
filled.contour(volcano,
color.palette = terrain.colors,
main = "Topographic Contour Map Showing Elevation of Land",
xlab = "East-West", ylab = "North-South")

library(ggplot2)
# Simulated rainfall data
set.seed(123)
lat <- runif(50, 25, 30) # latitude range
lon <- runif(50, 75, 80) # longitude range
rain <- rnorm(50, mean = 100, sd = 20)
df <- data.frame(lon, lat, rain)
# Interpolation grid
library(akima)
interp_data <- with(df, interp(lon, lat, rain, duplicate = "mean"))
interp_df <- data.frame(
lon = rep(interp_data$x, times = length(interp_data$y)),
lat = rep(interp_data$y, each = length(interp_data$x)),
rain = as.vector(interp_data$z)
)
ggplot(interp_df, aes(lon, lat, z = rain)) +
geom_contour_filled(bins = 10) +
scale_fill_viridis_d(option = "C") +
labs(title = "Rainfall Contour Map", x = "Longitude", y = "Latitude") +
theme_minimal()
## Warning: Removed 321 rows containing non-finite outside the scale range
## (`stat_contour_filled()`).

# Simulated temperature data
set.seed(42)
lat <- runif(40, 20, 25)
lon <- runif(40, 75, 82)
temp <- 25 + 5 * sin(lat) + rnorm(40)
df <- data.frame(lon, lat, temp)
# Interpolation
interp_data <- with(df, interp(lon, lat, temp, duplicate = "mean"))
interp_df <- data.frame(
lon = rep(interp_data$x, times = length(interp_data$y)),
lat = rep(interp_data$y, each = length(interp_data$x)),
temp = as.vector(interp_data$z)
)
ggplot(interp_df, aes(lon, lat, z = temp)) +
geom_contour_filled(bins = 12) +
scale_fill_viridis_d(option = "A") +
labs(title = "Temperature Variation", x = "Longitude", y = "Latitude") +
theme_minimal()
## Warning: Removed 315 rows containing non-finite outside the scale range
## (`stat_contour_filled()`).

set.seed(999)
lat <- runif(60, 28.4, 28.8) # Delhi region
lon <- runif(60, 77.0, 77.6)
pm25 <- rnorm(60, mean = 150, sd = 30)
df <- data.frame(lon, lat, pm25)
interp_data <- with(df, interp(lon, lat, pm25, duplicate = "mean"))
interp_df <- data.frame(
lon = rep(interp_data$x, times = length(interp_data$y)),
lat = rep(interp_data$y, each = length(interp_data$x)),
pm25 = as.vector(interp_data$z)
)
ggplot(interp_df, aes(lon, lat, z = pm25)) +
geom_contour_filled(bins = 8) +
scale_fill_viridis_d(option = "E") +
labs(title = "PM2.5 Contour Map (Air Quality)", x = "Longitude", y = "Latitude") +
theme_minimal()
## Warning: Removed 351 rows containing non-finite outside the scale range
## (`stat_contour_filled()`).

# 1. Load Libraries
# You may need to install GSODR: install.packages("GSODR")
library(leaflet)
library(raster)
## Loading required package: sp
library(sf)
## Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
##
## intersect, select, union
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(GSODR)
# 2. Fetch and Prepare Real Weather Data
# Fetch weather data for the US, Canada, and Mexico for 2024
weather_data <- get_GSOD(years = 2024, country = c("US", "CA", "MX"))
# Clean the data: filter to one day, keep relevant columns, remove NAs
# CORRECTED LINE: Using lowercase 'latitude', 'longitude', and 'temp'
temp_data_one_day <- weather_data %>%
filter(MONTH == 6, DAY == 15) %>%
select(LATITUDE, LONGITUDE, TEMP) %>%
na.omit()
# Convert to an sf object, specifying the corrected column names
temp_sf <- st_as_sf(temp_data_one_day, coords = c("LONGITUDE", "LATITUDE"), crs = 4326)
# 3. Interpolate and Generate Contours
# Create a raster grid covering the extent of the data points
grid <- raster(extent(temp_sf), resolution = 0.5)
# Interpolate the temperature readings onto the grid
temp_raster <- rasterize(temp_sf, grid, field = 'TEMP', fun = function(x, ...) mean(x, na.rm = TRUE))
interpolated_raster <- disaggregate(temp_raster, fact = 4) %>%
resample(grid, method = "bilinear")
# Generate contour lines and convert to sf
contour_lines <- rasterToContour(interpolated_raster, nlevels = 12)
contour_lines_sf <- st_as_sf(contour_lines)
# IMPORTANT: Convert the temperature 'level' to numeric
contour_lines_sf$level <- as.numeric(contour_lines_sf$level)
# 4. Build the Interactive Map
# Create a diverging color palette for temperature
pal <- colorNumeric(palette = "RdYlBu", domain = contour_lines_sf$level, reverse = TRUE)
# Create the map
temperature_map <- leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
# Add the original weather station points
addCircleMarkers(
data = temp_sf,
radius = 3,
color = "black",
stroke = FALSE,
fillOpacity = 0.5
#popup = ~paste("Temp:", round(temp, 1), "°C")
) %>%
# Add the temperature contour polygons
addPolygons(
data = contour_lines_sf,
fillColor = ~pal(level),
color = "white",
weight = 1,
fillOpacity = 0.6,
#label = ~paste("Temp:", round(level, 1), "°C")
) %>%
# Add a legend
addLegend(
pal = pal,
values = contour_lines_sf$level,
title = "Temperature (°C)",
position = "bottomright"
)
# Display the map
temperature_map
# Load libraries
#library(ggplot2)
#library(gganimate)
#library(dplyr)
# Create synthetic data: x, y grid with elevation changing over time#
#set.seed(123)
#grid <- expand.grid(x = seq(-2, 2, length.out = 50),
# y = seq(-2, 2, length.out = 50),
# time = 1:20)
#grid$z <- with(grid, exp(-(x^2 + y^2)) * cos(time/3) * 100)
# Plot animated contour
#p <- ggplot(grid, aes(x, y, z = z)) +
# geom_contour_filled() +
#labs(title = "Animated Contour Map (Elevation over Time: {frame_time})",
# x = "Longitude", y = "Latitude") +
#transition_time(time) +
#ease_aes('linear')
#animate(p, nframes = 40, fps = 5, width = 600, height = 600, renderer = #gifski_renderer())
# Load libraries
library(ggplot2)
library(dplyr)
# 1. Create a synthetic "temperature surface"
set.seed(123)
grid <- expand.grid(
lon = seq(-100, -80, length.out = 50),
lat = seq(30, 45, length.out = 50)
)
# Define a smooth temperature field (not random noise only!)
grid$temp <- with(grid, 25 + 5*sin(lon/5) + 3*cos(lat/5))
# 2. Plot contour map
p <- ggplot(grid, aes(lon, lat, z = temp)) +
geom_contour_filled(bins = 10) +
labs(title = "Simulated Temperature Contours",
x = "Longitude", y = "Latitude", fill = "Temp (°C)") +
theme_minimal()
print(p)

library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:raster':
##
## select
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
# Use volcano dataset
z <- volcano
# Create a 3D surface plot with contours
plot_ly(z = ~z) %>%
add_surface(contours = list(
z = list(show=TRUE, usecolormap=TRUE, highlightcolor="limegreen", project=list(z=TRUE))
)) %>%
layout(title = "3D Volcano Surface with Contours")